//
//  ICChatMessageURLTextCell.m
//  ICome
//
//  Created by zhangrongwu on 2017/7/22.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICChatMessageURLTextCell.h"
#import "ICMessageModel.h"
#import "ICFaceManager.h"
#import "ICHeaderFile.h"

@interface ICChatMessageURLTextCell()<MLLinkLabelDelegate>

@end

@implementation ICChatMessageURLTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.chatLabel];
    }
    return self;
}




#pragma mark - Private Method


- (void)setModelFrame:(ICMessageFrame *)modelFrame
{
    [super setModelFrame:modelFrame];
    self.chatLabel.frame = modelFrame.chatLabelF;
    
    NSDictionary *key = @{};
    if (![ICTools stringEmpty:modelFrame.model.message.fileKey]) {
        key = [NSDictionary dictionaryWithJsonString:modelFrame.model.message.fileKey];
    }
    [self.chatLabel setAttributedText:[self commentsAttributedString:modelFrame.model.message.content URL:key[@"url"]]];
    
    //URL 手机号加线
    if (modelFrame.model.isSender) {
        self.chatLabel.textColor = MESSAGEURLDEFAULTCOLOR;
        self.chatLabel.linkTextAttributes = @{NSForegroundColorAttributeName:MESSAGEURLDEFAULTCOLOR,NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)};
    }else{
        self.chatLabel.textColor = MESSAGEDEFAULTCOLOR;
        self.chatLabel.linkTextAttributes = @{NSForegroundColorAttributeName:MESSAGEURLDEFAULTCOLOR,NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)};
    }
}

-(NSAttributedString *)commentsAttributedString:(NSString *)content URL:(NSString *)url {
    NSString *atUrl = url == nil ? @"":url;
    if (![ICTools stringEmpty:content]) {
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:content];
        attString.font = MessageFont;
        UIColor *highLightColor = [UIColor whiteColor];
        
        [attString setAttributes:@{NSForegroundColorAttributeName : highLightColor,NSFontAttributeName : MessageFont, NSLinkAttributeName :atUrl} range:[content rangeOfString:content]];
        
        return attString;
    }
    return [[NSMutableAttributedString alloc] init];
}

- (void)attemptOpenURL:(NSURL *)url
{
    BOOL safariCompatible = [url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"];
    if (safariCompatible && [[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警示" message:@"您的链接无效" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)urlSkip:(NSString *)url
{
    [self routerEventWithName:GXRouterEventURLSkip
                     userInfo:@{@"url"   : url
                                }];
}


#pragma mark - Getter and Setter
- (MLLinkLabel *)chatLabel
{
    if (!_chatLabel) {
        _chatLabel = [[MLLinkLabel alloc] init];
        _chatLabel.numberOfLines = 0;
        _chatLabel.font = MessageFont;
        _chatLabel.lineHeightMultiple = 1.0f;
        _chatLabel.beforeAddLinkBlock = nil;
        _chatLabel.allowLineBreakInsideLinks = NO;
        _chatLabel.linkTextAttributes = nil;
        _chatLabel.activeLinkTextAttributes = nil;
        _chatLabel.userInteractionEnabled = YES;
        //        _chatLabel.dataDetectorTypes = MLDataDetectorTypeURL|MLDataDetectorTypePhoneNumber;
        _chatLabel.delegate = self;
    }
    return _chatLabel;
}

/** 聊天文本链接URL iphone控制*/
- (void)didClickLink:(MLLink*)link linkText:(NSString*)linkText linkLabel:(MLLinkLabel*)linkLabel {
    if (![ICTools stringEmpty:link.linkValue]) {
        if ([ICTools regularExpressionWithURLString:link.linkValue]) {
            [self urlSkip:link.linkValue];
        }
    }
}

@end












